<?php
namespace jy_shifu;
defined('BY_JYA') or exit('error');
/*
*/
class member extends \plugin{
	
	/*
		会员注册
	*/
	public function register($member=array()){
		$update=$member;
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('师傅会员注册失败，缺少会员ID.');
		}
		$ispid=$this->get('member',array('mid'=>$member['mid'],'group_id'=>$member['group_id'],'uniacid'=>$_SESSION['uniacid']),array('id'));
		if($ispid){
			$this->update('member',$update,array('mid'=>$member['mid'],'group_id'=>$member['group_id'],'uniacid'=>$_SESSION['uniacid']));
			return $ispid['id'];
		}
		$this->insert('member',$member);
		return $this->insertid();
	}
	/*
		申请成为师傅
	*/
	public function apply_agent($pid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$apply=$this->get('apply',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid']));
		if($apply && !$apply['status']){
			exi('该师傅点你已申请过了！','error');
		}
		if(!$apply['id']){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'pid'=>$pid,
				'merchid'=>$_SESSION['merchid'],
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
		}else{
			$this->update('apply',array('status'=>0),array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid']));
			$apply_id=$apply['id'];
		}
		if(!$this->setting['become_check']){
			//师傅无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		return true;
	}
	/*
		审核师傅
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'status'=>1,
			);
			$this->update('member',$member_update,array('id'=>$apply['pid'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$member_update=array(
				'status'=>$status,
			);
			$this->update('member',$member_update,array('id'=>$apply['pid'],'uniacid'=>$_SESSION['uniacid']));
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
    public function getlist($lat_lng=array(),$maxjuli=0,$limit=0,$page=0){
        $setting=$this->setting;
        $params=array(
            'uniacid'=>$_SESSION['uniacid'],
            'status'=>1,
            'lat_lng <>'=>'',
        );
        $maxjuli=intval($maxjuli)?:$setting['fanwei'];
        $maxjuli=$maxjuli?:30;
        $pindex = max(1, intval($page));
        $psize = 500;
        $total='';
        $list=pdo_getslice('jy_shifu_member',$params,array($pindex,$psize),$total,array(),'','createtime desc');
        foreach($list as $k=>&$v){
            $v['lat_lng']=json_decode($v['lat_lng'],true);
            $v['nowjuli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
            $v['nowjuli']=intval($v['nowjuli']/1000);
            if($maxjuli && $maxjuli<$v['nowjuli']){
                unset($list[$k]);
            }
        }
        paixu($list,'nowjuli');
        if($limit){
            $list=array_slice($list,0,intval($limit));
        }else{
            $page=intval($page)?:1;
            $start=($page-1)*10;
            $list=array_slice($list,$start,10);
        }
        return $list;
    }
	/*
		禁用师傅
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复师傅
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}
}